package com.oceansoft.core.jxkh.controller;

import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.jxkh.domain.Khy;
import com.oceansoft.core.jxkh.service.IKhyService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.swing.text.html.parser.Entity;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by OS on 2017/12/11.
 */

@Controller
@RequestMapping("/khy")
public class KhyController {

    @Resource
    private IKhyService khyService;

    @RequestMapping(value="allKhyInfo.json")
    public void allKhyInfo(HttpServletRequest request, ModelMap modelMap){
       /* List<Khy> list = new ArrayList<>();
        list = khyService.findAllKhy();
        modelMap.addAttribute("list",list);
        System.out.println(list.get(0).getName());
        return "jump/khysz";*/
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("offset",offset);
        paramap.put("limit",limit);
        paramap.put("order",order);

        Pagination KhyPag= khyService.findAllKhy(paramap);
        modelMap.addAttribute("rows",KhyPag.getList());
        modelMap.addAttribute("total",KhyPag.getTotalRows());
    }

    @RequestMapping(value="editKhyInfo",method = RequestMethod.POST)
    public void editKhyInfo(HttpServletRequest request, ModelMap modelMap,Khy khy){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khyService.editKhyInfo(khy);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="findKhyInfo.html")
    public String findKhyInfo(int dbid,ModelMap modelMap){
        Khy khy = khyService.findKhyInfoByDbid(dbid);
        modelMap.addAttribute("khy",khy);
        return "jump/khysz_bj";
    }

    @RequestMapping(value="deleteKhyInfo.json")
    public void deleteKhyInfo(int dbid, ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khyService.deleteKhyInfoByDbid(dbid);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="addKhyInfo.json",method =RequestMethod.POST)
    public void addKhyInfo(Khy khy, ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khyService.addKhyInfo(khy);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="batchDeleteKhyInfo.json")
    public void batchDeleteKhyInfo(int[] dbids,ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khyService.batchDeleteKhyInfo(dbids);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

}
